<?php

/**
 * Category form.
 *
 * @package    emc
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class AdminCategoryForm extends BaseCategoryForm
{
  public function configure()
  {
  	$this->setWidgets(array(
			'name'					=>		new sfWidgetFormInput(),
			'parent_id'				=>		new sfWidgetFormSelect(array('choices' => $this->getCategoryList())),
			'description'			=>		new sfWidgetFormTextarea(),
			'image'					=>		new sfWidgetFormInput(),
			'href'					=>		new sfWidgetFormInput(),
			'is_active'				=>		new sfWidgetFormInputCheckbox(),
		));
				
		$this->widgetSchema->setNameFormat('category[%s]');
		$this->widgetSchema->setLabels(array(
			'name'					=>		'Tên',
			'parent_id'				=>		'Mục cha',
			'description'			=>		'Mô tả',
			'image'					=>		'Hình ảnh',
			'href'					=>		'Link',
			'is_active'				=>		'Khả dụng',
		));
  }	

  private function getCategoryList()
  {
  	$list = array(1 => 'Home');
  	$this->getChildCategories(1, '-', $list);
  	return $list;
  }

  private function getChildCategories($category_id, $prefix, &$list) 
  {
  	$q = Doctrine_Query::create()
  			->select('c.id, c.name')
  			->from('Category c')
  			->addWhere('c.is_active = true')
  			->addWhere('c.parent_id = ?', $category_id);
  	$result = array();
  	foreach ($q->fetchArray() as $row) {
  		$list[$row['id']] = $prefix.$row['name'];
  		$this->getChildCategories($row['id'], $prefix.'-', $list);
  	}
  }
}
